package com.gyx.lottery.service.activitystatus.impl;

import com.gyx.lottery.common.errorcode.ServiceErrorCodeConstants;
import com.gyx.lottery.common.exception.ServiceException;
import com.gyx.lottery.service.ActivityService;
import com.gyx.lottery.service.activitystatus.ActivityStatusManager;
import com.gyx.lottery.service.activitystatus.operator.AbstractActivityOperator;
import com.gyx.lottery.service.dto.ConvertActivityStatusDTO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
@Service
public class ActivityManagerImpl implements ActivityStatusManager {
    private static final Logger logger = LoggerFactory.getLogger(ActivityManagerImpl.class);
    @Autowired
    private Map<String, AbstractActivityOperator>operatorMap=new HashMap<>();
    @Autowired
    private ActivityService activityService;
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void handlerEvent(ConvertActivityStatusDTO convertActivityStatusDTO) {
        //校验操作器是否正常运转
        if(operatorMap.isEmpty())
        { logger.warn("operatorMap 为空");
         return ; }
        //处理人员与奖品
        Boolean update = false;
        Map<String, AbstractActivityOperator>currentMap=new HashMap<>(operatorMap);
        //处理人员，奖品
        update=  processCovertStatus(convertActivityStatusDTO,currentMap,1);
        //处理活动
        update= processCovertStatus(convertActivityStatusDTO,currentMap,2) || update;

        //进行redis缓存存放
        if(update){
            activityService.cacheAcitivity(convertActivityStatusDTO.getActivityId());
        }
    }

    private Boolean processCovertStatus(ConvertActivityStatusDTO convertActivityStatusDTO,
                                        Map<String, AbstractActivityOperator> currentMap,
                                        int sequence) {
        Iterator<Map.Entry<String, AbstractActivityOperator>> iterator =
                currentMap.entrySet().iterator();
        Boolean update=false;
        while(iterator.hasNext()){
        AbstractActivityOperator operator=iterator.next().getValue();
        if(operator.sequence()!=sequence//判断与传入的处理序列是否一致
            || !operator.needCovert(convertActivityStatusDTO)){//查看是否需要进行状态转换
            continue;
        }
        //进行状态转换
           if(!operator.covert(convertActivityStatusDTO))
           {
               logger.error("状态转换失败:operator:{}",operator.getClass().getName());
               throw new ServiceException(ServiceErrorCodeConstants.ACTIVITY_STATUS_COVERT_ERROR);
           }
           iterator.remove();
           update =true;
        }
        return update;
    }

    @Override
    public void rollbackHandlerevent(ConvertActivityStatusDTO convertActivityStatusDTO) {
            for(AbstractActivityOperator operator: operatorMap.values())
            {
                operator.covert(convertActivityStatusDTO);
            }
         activityService.cacheAcitivity(convertActivityStatusDTO.getActivityId());

    }
}
